package ch.bfh.swos.learnaid.rest.controller;

import java.util.Collection;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import ch.bfh.swos.learnaid.service.QListService;
import ch.bfh.swos.learnaid.service.dto.QListDTO;

@Controller
@RequestMapping("/qlists")
public class QListController {

    @Inject
    private QListService qListService;

    /**
     * Create
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public QListDTO createQList(@RequestBody QListDTO qList) {
        QListDTO createdQList= qListService.create(qList);
        System.out.println("QLists created with id = " + createdQList.getId());
        return createdQList;
    }

    /**
     * ReadAll
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Collection<QListDTO> getQLists() {
        System.out.println("Collection of QLists requested");
        return qListService.list();
    }

    /**
     * Read
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public QListDTO getQList(@PathVariable long id) {
        System.out.println("QLists requested with id = " + id);
        return qListService.read(id);
    }

    /**
     * Update
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    @ResponseBody
    public QListDTO updateQList(@RequestBody QListDTO qList, @PathVariable long id) {
        QListDTO updatedQList = qListService.update(qList);
        System.out.println("Question updated with id = " + updatedQList.getId());
        return updatedQList;
    }

    /**
     * Delete
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void deleteQList(@PathVariable long id) {
        QListDTO qList = qListService.read(id);
        qListService.delete(qList);
        System.out.println("Delete QList with id = " + id);
    }
}
